package com.tim.app.ui.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.application.library.net.StringResponseCallback;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.tim.app.R;
import com.tim.app.manager.WindowUtils;
import com.tim.app.server.api.API_User;
import com.tim.app.server.logic.UserManager;
import com.tim.app.server.result.RedbagListBean;
import com.tim.app.server.result.ResponseResult;
import com.tim.app.server.result.SignInBean;
import com.tim.app.server.result.SigninResult;
import com.tim.app.util.MyYAnimation;
import com.tim.app.util.ToastUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wdy on 2017/8/18.
 */

public class SignInDialog extends DialogFragment {
    private final String TAG = "SignInDialog";
    private TextView mTv_flower_one, mTv_day_one, mTv_flower_two, mTv_day_two, mTv_flower_three, mTv_day_three, mTv_flower_four, mTv_day_four,
            mTv_flower_five, mTv_day_five, mTv_flower_six, mTv_day_six, mTv_day_seven, mTv_commit;
    private ImageView mIv_flower_seven;
    private List<TextView> flowers;
    private List<TextView> days;
    private SignInBean.ResBean.HashmapBean date;
    private ImageView iv_close;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        date = getArguments().getParcelable("hashmap");
        flowers = new ArrayList<>();
        days = new ArrayList<>();
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity(), R.style.DialogStyle);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        View view = View.inflate(getActivity(), R.layout.dialog_signin, null);
        dialog.setContentView(view);
        initView(view);
        initDate();
        return dialog;
    }


    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == iv_close) {
                dismiss();
            } else if (v == mTv_commit) {
                signIn();
            }
        }
    };

    //签到
    private void signIn() {
        API_User.ins().signIn(TAG, date.getUser().getId() + "", new StringResponseCallback() {
            @Override
            public boolean onStringResponse(String resultValue, int errCode, String errMsg, int id, boolean formCache) {
                Log.e("tag", "resultValue====" + resultValue);
                if (errCode == ResponseResult.ResultStatus.OK || errCode == 140006) {
                    SigninResult signinResult = new Gson().fromJson(resultValue, SigninResult.class);

                    mTv_commit.setSelected(true);
                    mTv_commit.setText("已签到");
//                    flowers.get(date.getUser().getSignCount() - 1).setSelected(true);
//                    days.get(date.getUser().getSignCount() - 1).setSelected(true);
                    if (onDimissListener != null) {
                        onDimissListener.onDismiss();
                    }
                    UserManager.ins().saveLastSign(System.currentTimeMillis() + "");
                    if (errCode == 140006) {
                        ToastUtil.showToast(signinResult.getMessage());
                        mIv_flower_seven.setSelected(true);
                        mTv_day_seven.setSelected(true);
                        List<RedbagListBean.ResBean.ArraylistBean> list = new ArrayList();
                        RedbagListBean.ResBean.ArraylistBean redBagBean = new RedbagListBean.ResBean.ArraylistBean();
                        redBagBean.setId(signinResult.getRes().getPropertyList().get(0).getId());
                        redBagBean.setMoney(signinResult.getRes().getPropertyList().get(0).getPropertyNum());
                        redBagBean.setType("sign");
                        list.add(redBagBean);
                        WindowUtils.getInStance().setRedbagList(list);
                        WindowUtils.getInStance().setContext(getContext());
                        WindowUtils.getInStance().showPopupWindow();
                        ToastUtil.showToast("获得拼手气红包一个");
                        dismiss();
                    } else {
                        if (date.getUser().getSignCount() % 7 + 1 < 7) {
                            flowers.get(date.getUser().getSignCount() % 7).setSelected(true);
                            days.get(date.getUser().getSignCount() % 7).setSelected(true);
                        }
                    }
                } else {
                    ToastUtil.showToast("签到失败");
                }
//                JSONObject bean = null;
//                try {
//                    bean = new JSONObject(resultValue);
//                    if (errCode == ResponseResult.ResultStatus.OK) {
//                        mTv_commit.setSelected(true);
//                        mTv_commit.setText("已签到");
//                        flowers.get(date.getUser().getSignCount()).setSelected(true);
//                        days.get(date.getUser().getSignCount()).setSelected(true);
//                        if (onDimissListener != null) {
//                            onDimissListener.onDismiss();
//                        }
//                    }
//                    if (bean != null) {
//                        String message = bean.getString("message");
//                        ToastUtil.showToast(message);
//
//                    }
//
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }

                return false;
            }
        });
    }

    private void initView(View view) {
        iv_close = (ImageView) view.findViewById(R.id.iv_close);
        mTv_flower_one = (TextView) view.findViewById(R.id.tv_flower_one);
        mTv_day_one = (TextView) view.findViewById(R.id.tv_day_one);
        mTv_flower_two = (TextView) view.findViewById(R.id.tv_flower_two);
        mTv_day_two = (TextView) view.findViewById(R.id.tv_day_two);
        mTv_flower_three = (TextView) view.findViewById(R.id.tv_flower_three);
        mTv_day_three = (TextView) view.findViewById(R.id.tv_day_three);
        mTv_flower_four = (TextView) view.findViewById(R.id.tv_flower_four);
        mTv_day_four = (TextView) view.findViewById(R.id.tv_day_four);
        mTv_flower_five = (TextView) view.findViewById(R.id.tv_flower_five);
        mTv_day_five = (TextView) view.findViewById(R.id.tv_day_five);
        mTv_flower_six = (TextView) view.findViewById(R.id.tv_flower_six);
        mTv_day_six = (TextView) view.findViewById(R.id.tv_day_six);
        mIv_flower_seven = (ImageView) view.findViewById(R.id.iv_flower_seven);
        mTv_day_seven = (TextView) view.findViewById(R.id.tv_day_seven);
        mTv_commit = (TextView) view.findViewById(R.id.tv_commit);
        mTv_commit.setOnClickListener(onClickListener);
        iv_close.setOnClickListener(onClickListener);

    }

    private void initDate() {
        flowers.add(mTv_flower_one);
        flowers.add(mTv_flower_two);
        flowers.add(mTv_flower_three);
        flowers.add(mTv_flower_four);
        flowers.add(mTv_flower_five);
        flowers.add(mTv_flower_six);
        days.add(mTv_day_one);
        days.add(mTv_day_two);
        days.add(mTv_day_three);
        days.add(mTv_day_four);
        days.add(mTv_day_five);
        days.add(mTv_day_six);
        days.add(mTv_day_seven);
        int size = date.getSignArray().size();
        List<Integer> signArray = date.getSignArray();
        int signCount = date.getUser().getSignCount();
        for (int i = 0; i < size; i++) {
            flowers.get(i).setText(signArray.get(i) + "");
            if (signCount >= 6) {
                flowers.get(i).setText(signArray.get(signArray.size() - 1) + "");
            }
        }


        int i2 = (int) signCount / 7;
        int i1 = signCount % 7;
        for (int i = 0; i < 7; i++) {
            int i3 = i2 * 7 + i + 1;
            days.get(i).setText("第" + i3 + "天");
        }

        for (int i = 0; i < i1; i++) {
            if (i == 6) {
                mIv_flower_seven.setSelected(true);
            } else {
                flowers.get(i).setSelected(true);
            }
            days.get(i).setSelected(true);
        }
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);

    }

    @Override
    public void onResume() {
        super.onResume();

    }

    public void setOnDimissListener(OnDimissListener onDimissListener) {
        this.onDimissListener = onDimissListener;
    }

    private OnDimissListener onDimissListener;

    public interface OnDimissListener {
        void onDismiss();

    }
}
